//
//  NoPayViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/7.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "NoPayViewController.h"
#import "OrderHeadView.h"
#import "OrderFootView.h"
#import "OrderTableViewCell.h"
#import "orderModel.h"

@interface NoPayViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL loadMore;
@property (nonatomic, copy) NSString *nextP; //更多
@end

@implementation NoPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bgNavView.hidden = YES;
    self.dataArray = [NSMutableArray array];
    [self getData];
   // [self setUpTable];
}

- (void)getData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //待结算收益
    [dict setValue:[NSNumber numberWithInteger:1] forKey:@"orderType"];
    if (self.loadMore == YES) {
        [dict setValue:self.nextP forKey:@"nextPageInfo"];
    }
    [HttpRequest formPostPath:OrderList_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
        if (msgModel.status == 200) {
            NSArray *resultArray = Data[@"result"][@"currentPageResult"];
            if ([Data[@"result"][@"hasMore"]intValue]==1){
                //有更多数据
                self.loadMore = YES;
                self.nextP = [NSString stringWithFormat:@"%@",Data[@"result"][@"nextPageInfo"]];
                [self.tableView.mj_footer resetNoMoreData];
            }else
            {
                //没有更多数据
                self.loadMore = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            for (NSInteger i = 0; i < resultArray.count; i++) {
                OrderModel *model = [OrderModel mj_objectWithKeyValues:resultArray[i]];
                model.goodsArray = [NSMutableArray array];
                //            model.cellHeight =  (model.subs.count-1)*80+90;
                for (NSInteger j = 0; j < model.orderItemDetails.count; j++) {
                    GoodsModel *goodsM = [GoodsModel mj_objectWithKeyValues:model.orderItemDetails[j]];
                    [model.goodsArray addObject:goodsM];
                }
                [self.dataArray addObject:model];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }
        else
        {
            [self ShowHUDWithMessage:Data[@"code"]];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
    [self setUpTable];
}

- (void)getNewData
{
    self.loadMore = NO;
    [self.dataArray removeAllObjects];
    [self getData];
}

- (void)getMoreData
{
    if (self.loadMore) {
        [self getData];
    }else
    {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}

- (void)setUpTable
{
    if (self.tableView == nil) {
        if (@available(iOS 11.0, *)) {
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREENWIDTH, SCREENHEIGHT-BottomHeight_xs-50-TopBarHeight_xs) style:UITableViewStyleGrouped];
        } else {
            // Fallback on earlier versions
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREENWIDTH, SCREENHEIGHT-BottomHeight-50-TopBarHeight) style:UITableViewStyleGrouped];
        }
        self.tableView.backgroundColor = TotalWhite;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.rowHeight = 90;
        [self.tableView registerClass:[OrderTableViewCell class] forCellReuseIdentifier:@"OrderTableViewCell"];
        [self.tableView registerClass:[OrderFootView class] forHeaderFooterViewReuseIdentifier:@"OrderFootView"];
        [self.tableView registerClass:[OrderHeadView class] forHeaderFooterViewReuseIdentifier:@"OrderHeadView"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.tableFooterView = [UIView new];
        [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.tableView withView:self];
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        [self.view addSubview:self.tableView];
    }
}

#pragma mark -- table delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OrderHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderHeadView"];
    if (!headView) {
        headView = [[OrderHeadView alloc]initWithReuseIdentifier:@"OrderHeadView"];
    }
    OrderModel *model = self.dataArray[section];
    [headView getData:model];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 79;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    OrderFootView *footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderFootView"];
    if (!footView) {
        footView = [[OrderFootView alloc]initWithReuseIdentifier:@"OrderFootView"];
    }
     //footView.backMoney = @"backMoney";
    OrderModel *model = self.dataArray[section];
    [footView getData:model];
    if (section == self.dataArray.count-1) {
        footView.bottomV.hidden = NO;
    }
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    OrderModel *model = self.dataArray[section];
    if (model.showGainsInfo == YES) {
        if (model.deliveryType == 3) {
            if (section == self.dataArray.count-1) {
                return 135-65;
            }
            return 125-65;
        }
        else
        {
            if (section == self.dataArray.count-1) {
                return 135;
            }
            return 125;
        }
    }
    else
    {
        if (model.deliveryType == 3) {
            if (section == self.dataArray.count-1) {
                return 125-65;
            }
            return 115-65;
        }
        else
        {
            if (section == self.dataArray.count-1) {
                return 125;
            }
            return 115;
        }
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
    //return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    OrderModel *model = self.dataArray[section];
    return model.goodsArray.count;
   // return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    OrderModel *model = self.dataArray[indexPath.section];
    GoodsModel *goodModel = model.goodsArray[indexPath.row];
    [cell getModel:goodModel];
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
